package tw.com.book.core.task03;

import com.parse.LogInCallback;
import com.parse.ParseException;
import com.parse.ParseUser;
import com.parse.SaveCallback;
import com.parse.SignUpCallback;

import android.content.Context;
import android.content.SharedPreferences;
import android.widget.Toast;
import tw.com.book.UserActivity;
import tw.com.book.api.task03.Task03Service;
import tw.com.book.componet.Deglagte;
import tw.com.book.componet.IssueBussinessException;
import tw.com.book.componet.Msg;
import tw.com.book.componet.MsssageCode;
import tw.com.book.dto.Task003DTO;

public class Task03ServiceImpl implements Task03Service {

	@Override
	public void loginAccount(Task003DTO dto, final Context context)
			throws IssueBussinessException {

		final SharedPreferences share = context.getSharedPreferences(
				USER_SHARE, 0);

		// 判斷是否要記錄密碼
		if (dto.isRember()) {
			share.edit().putString(//
					EMAIL, dto.getEmail())
					.putBoolean(IS_REMBERED, dto.isRember())//
					.commit();//
		} else {
			share.edit().putString(EMAIL, "")
					.putBoolean(IS_REMBERED, dto.isRember())//
					.commit();//
		}

		ParseUser.logInInBackground(dto.getEmail(), dto.getPassowrd(),
				new LogInCallback() {
					public void done(ParseUser user, ParseException e) {
						final Deglagte deg = (Deglagte) context;
						if (user != null) {

							deg.putMessage(LOGGING_ACTION, null);

						} else {
							deg.putMessage(LOGGING_ACTION,
									new IssueBussinessException(Msg.E002));
						}
					}
				});
	}

	@Override
	public void registerAccount(Task003DTO dto, final Context context)
			throws IssueBussinessException {
		final ParseUser user = new ParseUser();
		user.setUsername(dto.getEmail());
		user.setPassword(dto.getPassowrd());
		user.setEmail(dto.getEmail());

		user.signUpInBackground(new SignUpCallback() {
			public void done(ParseException e) {
				final Deglagte deg = (Deglagte) context;
				if (e == null) {
					deg.putMessage(LOGGING_ACTION, null);
				} else {
					deg.putMessage(LOGGING_ACTION, new IssueBussinessException(
							Msg.E003));
				}
			}
		});

	}

	@Override
	public void initValue(Task003DTO dto, Context context)
			throws IssueBussinessException {

		SharedPreferences share = context.getSharedPreferences(USER_SHARE, 0);
		final String email = share.getString(EMAIL, "");
		final boolean isRemberEd=share.getBoolean(IS_REMBERED, false);
		dto.setEmail(email);
		dto.setRember(isRemberEd);
		;

	}

	@Override
	public void signAccount(Task003DTO dto, final Context context)
			throws IssueBussinessException {

		final ParseUser user = new ParseUser();
		user.setEmail(dto.getEmail());
		user.setUsername(dto.getEmail());
		user.setPassword(dto.getPassowrd());
		user.signUpInBackground(new SignUpCallback() {

			@Override
			public void done(ParseException e) {
				final Deglagte deg = (Deglagte) context;
				if (e == null) {
					deg.putMessage(SIGN_ACTION, null);
				} else {
					if (e.getCode() == 202) {
						deg.putMessage(SIGN_ACTION,
								new IssueBussinessException(Msg.W002));
					}

				}

			}
		});

		// TODO Auto-generated method stub

	}
}
